<?php

	/**
    * @framework : My Framework
    *
    * @author: fatangel26@gmail.com - Nguyễn Bảo Trung.
    *
    */

	class model_menu extends my_model
	{
	    protected $_table;

		public function __construct()
        {
            parent::__construct();

            $this->_table = 'tbl_menu';
        }

        public function getAllMenu()
        {
            $this->order("menu_order","asc");
            $this->where(array('menu_status' => 1));
            $this->get($this->_table);

            if($this->num_rows() > 0)
                return $this->fetch_all();
            return FALSE;
        }

        private function checkHasChild($menus, $menu_id)
        {
            foreach($menus as $menu):

                if($menu['menu_parent'] == $menu_id)
                    return TRUE;

            endforeach;

            return FALSE;
        }

        public function buildTreeMenus($menus, $parent_id = 0, $level = 1)
        {

            $html = '';

            foreach($menus as $menu):

                if($menu['menu_parent'] == $parent_id):
                    if($menu['menu_parent'] == 0)
                    {
                        $html .= '<div class="main_beer">';
                        $html .= "<div class='main_menu_top'>".mb_strtoupper($menu['menu_name'],'UTF-8')."</div>";
                        $html .= "<div class='main_menu_between'><ul class='left_menu'>";
                    }
                    elseif($level == 2)
                    {
                        $html .= "<li class='odd'><a href='"._BASE_URL."index.php?module=product&slug=".$menu['slug']."'>".mb_convert_case($menu['menu_name'],MB_CASE_TITLE, 'UTF-8')."</a>";
                    }
                    else
                    {
                        $html .= "<ul class='child_menu' style='margin-left:".$level * 4 ."px!important'>";
                        $html .= "<li class='odd'><a href='"._BASE_URL."index.php?module=product&slug=".$menu['slug']."'>".mb_convert_case($menu['menu_name'],MB_CASE_TITLE, 'UTF-8')."</a></li>";
                    }

                    if($this->checkHasChild($menus, $menu['menu_id']))
                    {
                        $html .= $this->buildTreeMenus($menus, $menu['menu_id'], $level+1);
                    }

                    if($level != 2)
                    {
                        $html .= "</ul>";
                    }

                    if($level == 2)
                    {
                        $html .= "</li>";
                    }

                    if($menu['menu_parent'] == 0)
                    {
                        $html .= "</div>";
                        //$html .= "<div class='main_menu_futter'></div>";
                        $html .= "</div>";
                    }

                endif;

            endforeach;

            return $html;

        }

        public function getMenuSearch()
		{
			$strSQL = "SELECT menu_name, slug FROM $this->_table WHERE menu_parent != 0 AND menu_status = 1";

			$this->query($strSQL);

			if($this->num_rows() > 0)
				return $this->fetch_all();
			return FALSE;
		}

        public function getAllChilds($slug)
        {
            $this->where(array('menu_status' => 1));
            $this->get($this->_table);
            $menus = $this->fetch_all();
            /*echo "<pre>";print_r($menus);*/

            $condition = array(
                'slug'          => $slug,
                'menu_status'   => 1
            );

            $this->where($condition);
            $this->get($this->_table);
            $menu = $this->fetch_one();
            /*echo "<pre>";print_r($menu);*/
            if($this->num_rows() > 0)
            {
                if($this->checkHasChild($menus, $menu['menu_id']))
                {
                    return $this->getChilds($menus,$menu['menu_id']);
                }
                else
                {
                    return FALSE;
                }
            }
            else
            {
                return FALSE;
            }
        }


        public function getChilds($menus, $menu_id)
        {
            $childs_id = '';

            $count = 0;

            foreach($menus as $menu):

                if($menu['menu_parent'] == $menu_id):

                    if($count == 0):
                        $childs_id .= $menu_id.",";
                    endif;

                    $childs_id .= $menu['menu_id'].",";

                    if($this->checkHasChild($menus, $menu['menu_id']))
                    {
                        $childs_id .= $this->getChilds($menus, $menu['menu_id']);
                    }

                endif;

            endforeach;

            return $childs_id;
        }

        public function getMenuName($data = array())
        {
            $condition = array(
                'slug'          => $data['slug'],
                'menu_status'   => 1
            );

            $this->where($condition);

            $this->get($this->_table);

            if($this->num_rows() > 0)
                return $this->fetch_one();
            return FALSE;
        }
	}


    /*End of model_menu.php*/
    /*Location: my_framework/models/model_menu.php */